/*
 * @Description: 描述信息
 * @Version: 版本号
 * @Author: xuebao
 * @Date: 2023-12-10 16:01:41
 * @LastEditors: xuebao
 * @LastEditTime: 2023-12-11 22:41:43
 */
import { Button, Form, Input, App } from 'antd'
import { LockOutlined, UserOutlined } from '@ant-design/icons'
import styles from './login.module.less'
import api from '@/api/index'
import { Login } from '@/types/api'
import { setItem } from '@/utils/storage'
import { useState } from 'react'
import { useStore } from '@/store'

/**
 * Rmx0930
 * 476653
 * @returns
 */
export default function LoginF() {
  const [loading, setLoading] = useState(false)
  const updateToken = useStore((state) => state.updateToken)
  // const [form] = Form.useForm()
  // useEffect(() => {
  //   form.setFieldsValue({ userName: 'jack', userPwd: 24 })
  // }, [])
  const { message } = App.useApp()
  const onFinish = async (values: Login.params) => {
    try {
      setLoading(true)
      let data: any = await api.login(values)
      setLoading(false)
      message.success('登陆成功')
      setItem('token', data)
      updateToken(data)
      const params = new URLSearchParams(location.search)
      setTimeout(() => {
        location.href = params.get('callback') || '/welcome'
      })
    } catch (error) {
      setLoading(false)
    }
  }
  return (
    <div className={styles.login}>
      <div className={styles.loginWrapper}>
        <div className={styles.title}>系统登陆</div>
        {/* form={form} */}
        <Form
          name='basic'
          initialValues={{
            userName: 'Rmx0930',
            userPwd: '476653',
          }}
          onFinish={onFinish}
          autoComplete='off'
        >
          <Form.Item name='userName' rules={[{ required: true, message: 'Please input your username!' }]}>
            <Input prefix={<UserOutlined className='site-form-item-icon' />} placeholder='请输入用户名' />
          </Form.Item>

          <Form.Item name='userPwd' rules={[{ required: true, message: 'Please input your password!' }]}>
            <Input.Password prefix={<LockOutlined className='site-form-item-icon' />} placeholder='请输入密码' />
          </Form.Item>

          <Form.Item>
            <Button type='primary' htmlType='submit' block loading={loading}>
              登陆
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  )
}
